package br.com.servlet.controller.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class AutorizadorInterceptor extends HandlerInterceptorAdapter 
{
	@Override
	public boolean preHandle( HttpServletRequest request, HttpServletResponse response, Object controller ) 
			throws Exception 
	{
		
		String uri = request.getRequestURI( );
		
		if( uri.endsWith( "loginForm"        ) || uri.endsWith( "efetuaLogin" ) || uri.contains( "resources"   ) || 
			uri.endsWith( "gson-games"       ) || uri.endsWith( "agendamento" ) || uri.endsWith( "gson-contos" ) ||
			uri.endsWith( "cadastro-usuario" ) || uri.endsWith( "conto-login" ) )
		{
			return true;
		}

		if( request.getSession( ).getAttribute( "usuarioLogado" ) != null ) 
		{
			return true;
		}

	    response.sendRedirect( "loginForm.jsp" );
	    
		return false;
	}
}